/*
 * Copyright 2007 - The JDPF Project Team (http://www.jdpf.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdpf.core.kernel.elements;


/**
 * Represents the net <em>Input Arc</em>. The input arc is wiring a place
 * to a transition. An input arc can include an expression to be evaluated
 * in order to enable or not the link.
 * 
 * @author Paolo Ciccarese
 */
@SuppressWarnings("serial")
public class InputArc extends AbstractArc {

	/**
	 * The input can be diabled/enabled according to 
	 * an expression.
	 */
	private ArcExpression expression;
	
	/**
	 * In the case of an aggregator, it represent the order of this
	 * input in the list of the inputs.
	 */
	private int order;
	
	/**
	 * Full constructor
	 * @param id			The unique id of the input arc
	 * @param fromPlace		The provanance Place
	 * @param toTransition	The target Transition
	 */
	public InputArc(final String id, final Place fromPlace, 
			final ITransition toTransition) {
		super(id, fromPlace, toTransition);
		
		// Wiring
		fromPlace.addOutputArc(this);
		toTransition.addInputArc(this);
	}
	
	public InputArc(){
		super("default");
	}

	/**
	 * @return the expression
	 */
	public ArcExpression getExpression() {
		return expression;
	}

	/**
	 * @param expression the expression to set
	 */
	public void setExpression(ArcExpression expression) {
		this.expression = expression;
	}
	
	public boolean canConsumeToken() {
		return getPlace().getToken()!=null;
	}
	
	public boolean isEnabled() {
		if(getPlace().getToken()!=null) 
			return expression.evaluate();
		else
			return false;
	}
	
	public String toString(){
		return "InputArc "+this.getId()+", "+this.getName()+": source-"+
			this.getPlace().toString()+" destination-"+
			this.getTransition().toString();
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}
}
